package com.itheima.controller.backend.Service.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.controller.backend.Service.SetmealService;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;


@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    @Transactional
    public void addSetmeal(Setmeal setmeal, String checkgroupIds){
        Integer[] checkitemIdsArray = StringToArrayList(checkgroupIds);
        setmealMapper.addSetmeal(setmeal);
        for (Integer checkitemId : checkitemIdsArray) {
            setmealMapper.addSetmealAndCheckItem(setmeal.getId(),checkitemId);
        }
        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

    }

    @Override
    @Transactional
    public void edit(Setmeal setmeal, String checkGroupIds){
        Integer[] checkitemIdsArray = StringToArrayList(checkGroupIds);
        setmealMapper.edit(setmeal);
        setmealMapper.deleteByIdFromCheckGroupAndSetmeal(setmeal.getId());
        for (Integer checkitemId : checkitemIdsArray) {
            setmealMapper.addSetmealAndCheckItem(setmeal.getId(),checkitemId);
        }
    }

    @Override
    @Transactional
    public Setmeal findById(Integer id){
        return setmealMapper.findById(id);
    }



    @Override
    @Transactional
    public PageResult findPage(QueryPageBean queryPageBean, Integer currentPage, Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<Setmeal> setmealList = setmealMapper.findPage(queryPageBean);
        Page<Setmeal> setmeals = (Page) setmealList;
        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    @Override
    @Transactional
    public String uploadImage(MultipartFile imgFile){
        log.info("文件上传中：{}", imgFile);
        try {
                String originalFilename = imgFile.getOriginalFilename();
                String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
                String objectName = UUID.randomUUID().toString()+extension;

                String filePath = aliOssUtil.upload(imgFile.getBytes(),objectName);
                redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,filePath);
                return filePath;
        } catch (IOException e) {
                log.error("文件上传失败：{}", e);
        }
        return "文件上传失败";
    }






    private Integer[] StringToArrayList(String checkitemIds){
        Integer[] checkitemIdsArray = new Integer[checkitemIds.split(",").length];
        for (int i = 0; i < checkitemIdsArray.length; i++) {
            checkitemIdsArray[i] = Integer.parseInt(checkitemIds.split(",")[i]);
        }
        return checkitemIdsArray;
    }
}
